package com.game.doudizhu.service;

import com.game.doudizhu.model.DailyTask;
import com.game.doudizhu.model.Player;
import com.game.doudizhu.repository.DailyTaskRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.util.*;

@Service
public class DailyTaskService {
    
    @Autowired
    private DailyTaskRepository taskRepository;
    
    @Autowired
    private PlayerService playerService;
    
    private final List<Map<String, Object>> TASK_TEMPLATES = createTaskTemplates();
    
    private List<Map<String, Object>> createTaskTemplates() {
        List<Map<String, Object>> templates = new ArrayList<>();
        
        // 赢得游戏任务
        Map<String, Object> winGamesTask = new HashMap<>();
        winGamesTask.put("type", "WIN_GAMES");
        winGamesTask.put("requirement", 3);
        winGamesTask.put("reward", 100);
        templates.add(winGamesTask);
        
        // 参与游戏任务
        Map<String, Object> playGamesTask = new HashMap<>();
        playGamesTask.put("type", "PLAY_GAMES");
        playGamesTask.put("requirement", 5);
        playGamesTask.put("reward", 50);
        templates.add(playGamesTask);
        
        // 地主胜利任务
        Map<String, Object> landlordWinTask = new HashMap<>();
        landlordWinTask.put("type", "WIN_AS_LANDLORD");
        landlordWinTask.put("requirement", 2);
        landlordWinTask.put("reward", 150);
        templates.add(landlordWinTask);
        
        return Collections.unmodifiableList(templates);
    }
    
//    @Scheduled(cron = "0 0 0 * * ?") // 每天凌晨执行
    public void generateDailyTasks() {
        LocalDate today = LocalDate.now();
        List<Player> players = playerService.getAllPlayers();
        
        for (Player player : players) {
            for (Map<String, Object> template : TASK_TEMPLATES) {
                DailyTask task = new DailyTask();
                task.setPlayer(player);
                task.setTaskType((String) template.get("type"));
                task.setRequirement((Integer) template.get("requirement"));
                task.setReward((Integer) template.get("reward"));
                task.setProgress(0);
                task.setDate(today);
                
                taskRepository.save(task);
            }
        }
    }
    
    public void updateTaskProgress(Player player, String taskType, int increment) {
        List<DailyTask> tasks = taskRepository.findByPlayerAndDateAndTaskType(
            player, LocalDate.now(), taskType);
            
        for (DailyTask task : tasks) {
            if (!task.getCompleted()) {
                task.setProgress(task.getProgress() + increment);
                if (task.getProgress() >= task.getRequirement()) {
                    completeTask(task);
                }
                taskRepository.save(task);
            }
        }
    }
    
    private void completeTask(DailyTask task) {
        task.setCompleted(true);
        Player player = task.getPlayer();
        player.setScore(player.getScore() + task.getReward());
        playerService.updatePlayer(player);
    }
} 